package com.wuming.caiwu.util;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.*;

/**
 * 生活圈 加签工具类
 */
public class SignUtil {

    public static String createSign(Map map, String secret) {
        StringBuffer sf = new StringBuffer();
        Map<String, String> sortParams = new TreeMap<String, String>(map);
        if (sortParams != null && sortParams.keySet().size() > 0) {
            Iterator iterator = sortParams.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry<String, String>) iterator.next();
                sf.append(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }

        String s = sf.toString();
        //移除最后一个&，如果是md5加密，还要加上secret
        String s2 = s.substring(0, s.length() - 1) + secret;
        System.out.println(s2);
        String sign = DigestUtils.md5Hex(s2).toUpperCase();
        return sign;
    }

    public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        params.put("result_code", "200");
        params.put("result_message", "成功");
        params.put("data", "{\"attach\":\"\",\"cash_fee\":1.00,\"cashier_id\":0,\"device_no\":\"0\",\"fee\":0.00,\"merchant_order_sn\":\"1412124302202102092159493698\",\"order_price\":1.00,\"pay_time\":1612879206,\"platform_order_no\":\"4200000807202102096920144309\",\"store_id\":1085271,\"total_fee\":1.00,\"trade_no\":\"2021020922000001173197025038N\",\"user_id\":\"ogRDxv2C5PrFVAwgOiPYwI5awecM\",\"user_logon_id\":\"\"}");
        System.out.println(createSign(params, "e2f208d3d26beaff8601580f5a689978"));
    }

}
